import {
  batchCreate,
  batchDel,
  create,
  del,
  detail,
  download,
  edit,
  page,
  type R,
  upload
} from "@/api";
import { http } from "@/utils/http";

const path = "/api/v1/system/user";
const customPath = "/api/v1/user";
const passwordPath = "/api/v1/password";

enum Index {
  checkUniqueness = "/check/uniqueness",
  roleCustom = "/role",
  pageCustom = "/page",
  createCustom = "/create",
  editCustom = "/edit",
  reset = "/reset",
  changePassword = "/change",
  avatar = "/avatar",
  editStatusCustom = "/edit/state"
}

export type PageResult = {
  total: number;
  current: number;
  size: number;
  records: Array<Record>;
};

export type Record = {
  id: string;
  name: string;
  username: string;
  avatar: string;
  birthday: string;
  sex: number;
  email: string;
  phone: string;
  status: number;
  dept: {
    name: string;
    id: string;
  };
  remark: string;
  createTime: string;
};

export type ListRoleIdCustomRecord = Array<string>;

export const apiPage: (data?: object) => Promise<R> = (data?: object) =>
  page(path, data);

export const apiDetail = (id?: string) => detail(path, id);

export const apiCreate = (data?: object) => create(path, data);

export const apiBatchCreate = (data?: object) => batchCreate(path, data);

export const apiEdit = (data?: object) => edit(path, data);

export const apiDel = (id?: string) => {
  return del(path, id);
};

export const apiBatchDel = (data?: object) => batchDel(path, data);

export const apiDownload = (params?: object, data?: object, isXlsx = false) =>
  download(path, params, data, isXlsx);

export const apiUpload = (data?: object) => upload(path, data);

export const checkUniqueness = (data?: object) =>
  http.get(`${path}${Index.checkUniqueness}`, { data: data });

export const editStatusCustom = (data?: object) =>
  http.put(`${customPath}${Index.editStatusCustom}`, { data: data });

export const pageCustom = (data?: object) =>
  http.post<R<PageResult>, {}>(`${customPath}${Index.pageCustom}`, {
    data: data
  });

export const createCustom = (data?: object) =>
  http.post<R, {}>(`${customPath}${Index.createCustom}`, { data: data });

export const editCustom = (data?: object) =>
  http.post<R, {}>(`${customPath}${Index.editCustom}`, { data: data });

export const resetPassword = (data?: object) =>
  http.put<R, {}>(`${passwordPath}${Index.reset}`, { data: data });

export const changePassword = (data?: object) =>
  http.put(`${path}${Index.changePassword}`, { data: data });

export const uploadAvatar = (data?: any, id?: string) => {
  const formData = new FormData();
  formData.append("file", data);
  return http.post<R, {}>(`${customPath}${Index.avatar}`, {
    headers: {
      "Content-type": "multipart/form-data"
    },
    data: formData,
    params: {
      id: id
    }
  });
};

export const roleIdCustom = (id: string) =>
  http.get<R<ListRoleIdCustomRecord>, {}>(
    `${customPath}${Index.roleCustom}/${id}`
  );
